<?php
include("../myzhuaqu.php");
require_once('../../../../vendor/autoload.php');
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\WebDriverExpectedCondition;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverWait;

$options = new ChromeOptions();
//$options->addArguments(['--headless']);  //不出现浏览器
$options->addArguments(['--disable-blink-features=AutomationControlled']);
$options->addArguments(['--disable-extensions']);
$options->addArguments(['--disable-gpu']);
$options->addArguments(['--disable-infobars']);
$options->addArguments(['--disable-notifications']);
$options->addArguments(['--disable-popup-blocking']);
$options->addArguments(['--disable-web-security']);
$options->addArguments(['--ignore-certificate-errors']);
$options->addArguments(['--no-sandbox']);
$options->setExperimentalOption("useAutomationExtension", false);
$options->setExperimentalOption("excludeSwitches", ['enable-automation']);
//$options->addArguments(['--start-maximized']);
//chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
$options->addArguments(['--user-data-dir=/d/temp']);

$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
$driver = RemoteWebDriver::create('http://localhost:9515', $capabilities);
//$driver->setExperimentalOption('excludeSwitches', ['enable-automation', 'useAutomationExtension']);
//$driver->execute_cdp_cmd('Network.setUserAgentOverride', '{"userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"}');
//https://www.douyin.com/video/7334611830842674470
$driver->get('https://www.douyin.com/?recommend=1');
$script = 'Object.defineProperty(navigator, "webdriver", {get: () => undefined,});';
$driver->executeScript($script);
//$driver->setExperimentalOption('excludeSwitches', ['enable-automation']);
//$element = $driver->findElement(WebDriverBy::id('fm-login-id'));
//$element->sendKeys("yzs1013");
//$element = $driver->findElement(WebDriverBy::id('fm-login-password'));
//$element->sendKeys("~159456!aA");
//$driver->findElement(WebDriverBy::className('password-login'))->click();
$wait = new WebDriverWait($driver, 10);
$driver->get("https://www.douyin.com/video/7334611830842674470");
$wait->until(
function ($driver) {
    return $driver->findElement(WebDriverBy::cssSelector('avatar-component-avatar-container'));
}
);
$cookies = $driver->manage()->getCookies();
$cookiestr="";
for($i=0;$i<count($cookies);$i++){
$cookiestr.=$cookies[$i]->getName()."=".$cookies[$i]->getValue().";";
}
//$driver->quit();
